package edu.bulsu.coe;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Configurations{
	DocumentBuilderFactory factory;
	DocumentBuilder builder;
	File file;
	Document doc;
	NodeList list;
	HashMap<String, String> properties = new HashMap<String, String>();
	
	public Configurations(String fileLocation) throws ParserConfigurationException, IOException, SAXException{
		file = new File(fileLocation);
		factory = DocumentBuilderFactory.newInstance();
		builder = factory.newDocumentBuilder();
		doc = builder.parse(file);
		list = doc.getElementsByTagName("configuration");
		for(int i = 0; i < list.getLength(); i++){
			Node node = list.item(i);
			if(node.getNodeType() == Node.ELEMENT_NODE){
				Element e = (Element) node;
				NodeList inner = e.getChildNodes();
				for(int j = 0; j < inner.getLength(); j++){
					Node innernode = inner.item(j);
					if(innernode.getNodeType() == Node.ELEMENT_NODE){
						Element f = (Element) innernode;
						String propertyName = f.getNodeName();
						String propertyValue = e.getElementsByTagName(propertyName).item(0).getTextContent();
						properties.put(propertyName, propertyValue);
					}
				}
			}
		}
	}
	
	public String getParams(String paramName){
		return properties.get(paramName);
	}
}
